<?php
/**
 * 参    数：
 * 作    者：lht
 * 功    能：OAth2.0协议下第三方登录数据报表
 * 修改日期：2013-12-13
 */
namespace User\Controller;

use Common\Controller\AdminbaseController;

class OauthadminController extends AdminbaseController {
	
	// 后台第三方用户列表
	public function index(){
        $name = I('name','');
        if($name != ''){
            $where['name'] = array('like', "%$name%");
        }
        $ordertype = I('order','');
        if($ordertype == ''){
            $order = "create_time DESC,login_times DESC";
        }
        if($ordertype == 1){
            $order = "create_time DESC,login_times DESC";
        }
        if($ordertype == 2){
            $order = "login_times DESC,create_time DESC";
        }
        $where['status'] = 1;
		$oauth_user_model=M('OauthUser');
        $count=$oauth_user_model->where($where)->count();
		$page = $this->page($count, 20);
		$lists = $oauth_user_model
		->where($where)
		->order("$order")
		->limit($page->firstRow . ',' . $page->listRows)
		->select();
        foreach($lists as $key=>$list){
            $user_id = $list['id'];
            $count = M('collect_cards_activity')->where("user_id = $user_id")->count();
            $lists[$key]['a_count'] = $count;
        }
		$this->assign("page", $page->show('Admin'));
		$this->assign('lists', $lists);
		$this->assign('order', $ordertype);
		$this->display();
	}
    // 后台第三方用户列表
    public function browse_index(){
        $name = I('name','');
        $where = "1 = 1 and u.id NOT IN(8,9,10,11,14,15,16,17,20,26,40,220,3159,2911,3158,3166,3294,14,441,342)";
        if($name != ''){
            $where .= " and u.name like '%$name%'";
        }
        $ordertype = I('order','');
        if($ordertype == ''){
            $order = "bnum DESC";
        }
        if($ordertype == 1){
            $order = "u.create_time DESC,u.login_times DESC";
        }elseif($ordertype == 2){
            $order = "u.login_times DESC,u.create_time DESC";
        }elseif($ordertype == 3){
            $order = "bnum DESC";
        }
//        $where['status'] = 1;
//        $oauth_user_model=M('OauthUser');
//        $count=$oauth_user_model->where($where)->count();
//        $page = $this->page($count, 20);

        $count_sql = " SELECT COUNT(DISTINCT a.id) AS bnum,u.name,a.* FROM cmf_oauth_user u
                  LEFT JOIN  cmf_collect_cards_activity a ON a.user_id = u.id
                  WHERE  $where
                  GROUP BY a.user_id
                  ORDER BY $order";
        $count =  M()->query($count_sql);
        $count = sizeof($count);
        $page = $this->page($count, 20);

        $sql = "  SELECT COUNT(DISTINCT a.id) AS bnum,u.* FROM cmf_oauth_user u
                  LEFT JOIN  cmf_collect_cards_activity a ON a.user_id = u.id
                    WHERE  $where
                  GROUP BY a.user_id
                  ORDER BY $order
                 LIMIT $page->firstRow, $page->listRows";
        $lists = M()->query($sql);

        $this->assign("page", $page->show('Admin'));
        $this->assign('lists', $lists);
//        $this->assign('order', $ordertype);
        $this->display();
    }
	// 后台删除第三方用户绑定
	public function delete(){
		$id = I('get.id',0,'intval');
		if(empty($id)){
			$this->error('非法数据！');
		}
		$result = M("OauthUser")->where(array("id"=>$id))->delete();
		if ($result!==false) {
			$this->success("删除成功！");
		} else {
			$this->error('删除失败！');
		}
	}

    // 后台冻结第三方用户
    public function dongjie(){
        $id = I('get.id',0,'intval');
        if(empty($id)){
            $this->error('非法数据！');
        }
        $data ['dongjie'] = 0;
        $result = M("OauthUser")->where(array("id"=>$id))->save($data);
        if ($result!==false) {
            $this->success("冻结成功！", U("oauthadmin/index"));
        } else {
            $this->error('冻结失败！');
        }
    }

    // 后台冻结第三方用户
    public function canceldongjie(){
        $id = I('get.id',0,'intval');
        if(empty($id)){
            $this->error('非法数据！');
        }
        $data ['dongjie'] = 1;
        $result = M("OauthUser")->where(array("id"=>$id))->save($data);
        if ($result!==false) {
            $this->success("取消冻结成功！", U("oauthadmin/index"));
        } else {
            $this->error('取消冻结失败！');
        }
    }
	public function get_activity(){
        $id = I('get.id',0,'intval');
        if(empty($id)){
            $this->error('非法数据！');
        }
    }
	
}